package com.sojson.config.mybatis.interceptor.bean;

import java.io.IOException;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.sojson.util.token.TokenUtil;

import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;

/**
 * Mybatis租户处理器
 * 
 * @author liu
 * @date 2022-01-20
 */
@Slf4j
public class CustomTenantLineHandler implements TenantLineHandler {

    private static final String COLUMN_NAME = "test";

    /**
     * 获取租户字段名
     */
    @Override
    public String getTenantIdColumn() {
        return COLUMN_NAME;
    }

    /**
     * 获取租户ID
     */
    @Override
    public Expression getTenantId() {
        try {
            return new StringValue(TokenUtil.getUserId() + "");
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 是否忽略这张表
     */
    @Override
    public boolean ignoreTable(String tableName) {
        return false;
    }

}